<?php

namespace App\Http\Controllers;

use App\News;
use Illuminate\Http\Request;
use App\City;
use App\Park;
use App\Workshop;
use App\Area;

class IndexController extends Controller
{
    //首页
    public function index(Request $request)
    {
        session('cityId',City::firstOrFail()->id);
        if($request->changeCity){//城市切换
            session(['cityId' => $request->changeCity]);
            session(['cityName'=>City::findOrFail($request->changeCity)->title]);
        }
        $cityId = session('cityId');
        $parks = Park::where('city_id','=',$cityId)->where('is_hot','=','1')->get();

        return view('index',['parks'=>$parks]);
    }

    //热门搜索
    public function hotsale(Request $request)
    {
        $parkId = $request->parkId;
        //查询区域列表
        $area=City::where('parent_id','=',session('cityId'))->get();
        $area1=City::whereIn('parent_id',$area->pluck('id')->all())->get();
        //获取面积信息
        $areas = Area::all();
        $workshop= Workshop::join('park','workshop.park_id','=','park.id')
            ->where('workshop.park_id','=',$parkId)
            ->select('workshop.*','park.id')
            ->get();
        //热门搜索
        $parks = Park::where('city_id','=',session('cityId'))->where('is_hot','=','1')->get();
        return view('houselist', ['workLists' => $workshop,'area'=>$area,'area1'=>$area1,'areas'=>$areas,'parks'=>$parks]);
    }

    //正常搜索
    public function search(Request $request)
    {
        //关键字
        $keyword = $request->keyword;
        //查询区域列表
        $area=City::where('parent_id','=',session('cityId'))->get();
        $area1=City::whereIn('parent_id',$area->pluck('id')->all())->get();
        //获取面积信息
        $areas = Area::all();
        $workshop= Workshop::join('park','workshop.park_id','=','park.id')
            ->where('park.title','like','%'.$keyword.'%')
            ->select('workshop.*','park.id')
            ->get();
        //热门搜索
        $parks = Park::where('city_id','=',session('cityId'))->where('is_hot','=','1')->get();
        return view('houselist', ['workLists' => $workshop,'area'=>$area,'area1'=>$area1,'areas'=>$areas,'parks'=>$parks]);
    }
}
